<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height: 60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="addRole"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="list">
              <el-table-column label="序号" width="120" type="index" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template slot-scope="{ row }">
                  <el-button
                    size="small"
                    type="success"
                    @click="assignPermission(row.id)"
                  >分配权限</el-button>
                  <el-button
                    size="small"
                    type="primary"
                    @click="editRole(row.id)"
                  >编辑</el-button>
                  <el-button
                    size="small"
                    type="danger"
                    @click="delHandel(row.id)"
                  >删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="height: 60px"
            >
              <!-- 分页组件 -->
              <el-pagination
                :current-page="query.page"
                :page-sizes="[5, 10, 15, 20, 30]"
                :total="total"
                :page-size="query.pagesize"
                layout="total,sizes,prev,pager,next"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
              />
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <!-- 新增弹框 -->
      <el-dialog title="新增角色" :visible.sync="isShow" @close="close">
        <el-form
          ref="roleForm"
          :model="roleForm"
          :rules="rules"
          label-width="100px"
        >
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" @click="isShow = false">取消</el-button>
            <el-button
              size="small"
              type="primary"
              @click="hSubmit"
            >确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>
      <!-- 权限弹框 -->
      <assign-permissions ref="assign" />
    </div>
  </div>
</template>
<script>
import { getRoles, deleteRole, addRole, getRole, updateRole } from '@/api/role'
import assignPermissions from './assignPermissions.vue'
export default {
  name: 'Settings',
  components: { assignPermissions },

  data() {
    return {
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '角色名称不能为空', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '描述不能为空', trigger: 'blur' }
        ]
      },
      isShow: false,
      list: [],
      query: {
        page: 1,
        pagesize: 5
      },
      total: 0
    }
  },
  created() {
    this.loadRoles()
  },
  methods: {
    // 分配权限
    async assignPermission(id) {
      await this.$refs.assign.initPermission()
      await this.$refs.assign.getPermissonsById(id)
      this.$refs.assign.isShow = true
    },
    async editRole(id) {
      const { data } = await getRole(id)
      this.roleForm = data
      this.isShow = true
    },
    // 点击确定
    async hSubmit() {
      await this.$refs.roleForm.validate()
      if (this.roleForm.id) {
        await updateRole(this.roleForm)
      } else {
        await addRole(this.roleForm)
        this.total++
        this.query.page = Math.ceil(this.total / this.query.pagesize)
        console.log(this.query.page)
      }
      this.loadRoles()
      this.$message.success('操作成功')
      this.isShow = false
    },
    close() {
      this.roleForm = {}
      this.$refs.roleForm.clearValidate()
    },

    addRole() {
      this.isShow = true
    },
    async delHandel(id) {
      await this.$confirm('确认删除吗？')
      await deleteRole(id)
      this.$message.success('删除成功')
      if (this.list.length === 1 && this.query.page > 1) {
        this.query.page--
      }
      this.loadRoles()
    },
    handleCurrentChange(page) {
      this.query.page = page
      this.loadRoles()
    },
    handleSizeChange(size) {
      this.query.pagesize = size
      this.loadRoles()
    },
    async loadRoles() {
      const { data } = await getRoles(this.query)
      this.list = data.rows
      this.total = data.total
    }
  }
}
</script>
